<?php

namespace App\Http\Controllers\Front;

use App\Interfaces\UserCollectionService;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use App\Http\Requests\Front\collection\CollectionStoreRequest;



class UserCollectionController extends Controller
{

    public function index(Request $request,UserCollectionService $userCollectionService){


        $page =$request->get('page');
        $type = $request->get('type');

        $goodsCollection = $userCollectionService->lists(Auth::user()->id,'goods',$page);

        $shopCollection = $userCollectionService->lists(Auth::user()->id,'SHOP',$page);

        if ($type=='GOODS'||$type==''){
            $userCollection = $goodsCollection;
        }else{
            $userCollection = $shopCollection;
        }
        $filter = ['type'=>$type];

        return view('front.my.collection_index',compact(['userCollection','request','goodsCollection','shopCollection','filter']));
    }

    public function store(CollectionStoreRequest $request,UserCollectionService $userCollectionService){

        $user = Auth::user();

        $user_id = $user->id;

        if($userCollectionService->isInCollection($user_id,$request->get('type'),$request->get('reference_id'))){
            return response()->json(['success' => '0', 'message' => '此商品已经收藏过']);

        }


        $userCollection = $userCollectionService->add($user_id,$request->get('type'),$request->get('reference_id'));

        if ($userCollection) {
            return response()->json(['success' => '1', 'message' => '收藏成功']);
        }else{
            return response()->json(['success' => '0', 'message' => '收藏失败']);

        }
    }

    public function destroy(Request $request,UserCollectionService $userCollectionService){



        $cancel = $userCollectionService->cancel(Auth::user()->id,$request->get('type'),$request->get('reference_id'));



        if ($cancel) {
            return response()->json(['success' => '1', 'message' => '取消成功']);
        }else{
            return response()->json(['success' => '0', 'message' => '取消失败']);

        }


    }


}
